﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Management.Automation;
using SimpleNotify.PushLibrary;

namespace SimpleNotify.Powershell
{
    /// <summary>
    /// This is our "Push-Notification" custom PowerShell
    /// cmdlet.
    /// </summary>
    [Cmdlet(VerbsCommon.Push, "Notification", SupportsShouldProcess = false)]
    public class PushNotificationCmdlet : Cmdlet
    {
        private Implementation _implementation = new Implementation();

        private string _type;

        [Parameter(Mandatory = false, HelpMessage = "Notification type")]
        [ValidateSet("None","Information","Confirmation","Warning","Error")]
        public string Type
        {
            get { return _type; }
            set { _type = value.ToLower(); }
        }

        private string _text;

        [Parameter(Mandatory = true, HelpMessage = "Notification text")]
        public string Text
        {
            get { return _text; }
            set { _text = value; }
        }

        private string _title;

        [Parameter(Mandatory = false, HelpMessage = "Notification title")]
        public string Title
        {
            get { return _title; }
            set { _title = value; }
        }

        private string _client;

        [Parameter(Mandatory = true, HelpMessage = "Client to notify")]
        public string Client
        {
            get { return _client; }
            set { _client = value; }
        }

        private string _server;

        [Parameter(Mandatory = true, HelpMessage = "Hostname of the Server which runs the Notify Service")]
        public string Server
        {
            get { return _server; }
            set { _server = value; }
        }

        private string _data;

        [Parameter(Mandatory = false, HelpMessage = "Additional data")]
        public string Data
        {
            get { return _data; }
            set { _data = value; }
        }

        /// <summary>
        /// This is our main processing method, called
        /// once for each input record in the pipeline
        /// </summary>
        protected override void ProcessRecord()
        {
            NotificationType type = NotificationType.None;

            switch (_type)
            {
                case "information":
                    type = NotificationType.Information;
                    break;

                case "confirmation":
                    type = NotificationType.Confirmation;
                    break;

                case "warning":
                    type = NotificationType.Warning;
                    break;

                case "error":
                    type = NotificationType.Error;
                    break;
            }

            PushResult result = _implementation.PushNotification(_client, _server, _title, _text, type, _data);
            WriteObject(result, true);
        }
    }
}
